package com.javaweb.blogcite.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtil {
    private static final String ALI_DOMAIN = "https://tsycheese.oss-cn-guangzhou.aliyuncs.com/";

    public static String uploadImage(MultipartFile file) throws IOException {
        // 生成文件名
        String originalFilename = file.getOriginalFilename();
        String etc = FilenameUtils.getExtension(originalFilename);
        String uuid = UUID.randomUUID().toString().replace("-", "");
        String fileName = uuid + "." + etc;

        // 地域节点
        String endpoint = "http://oss-cn-guangzhou.aliyuncs.com";
        String accessKeyId = "LTAI5tGdpTiiErCdGVxfqV45";
        String accessKeySecret = "6N4MqCyr6wDULM7J4ZTTM6fweR9ZFq";
        // OSS 客户端对象
        OSS oosClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        oosClient.putObject("tsycheese", fileName, file.getInputStream());
        oosClient.shutdown();
        return ALI_DOMAIN + fileName;
    }
}
